﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using DEL.Webmonitor;
using SFL;

namespace BLL.Webmonitor
{
    public class GetInfos
    {
        InfoHitService InfoHitBLL = new InfoHitService();
        InfoOriginalService InfoOriginalBLL = new InfoOriginalService();
                
        HitPlan hp = new HitPlan();

        #region 从数据库读取数据到布隆过滤器

        public GetInfos()
        {
            if (!ComnTag.BloomFilterTag)
            {
                //WriteLog.WriteWorkLog("初始化布隆过滤器", "开始！");
                var num = hp.InitBloomFilter();                
                WriteLog.WriteWorkLog("初始化布隆过滤器", "成功完成！"+ num);
            }
            else
            {
                //WriteLog.WriteWorkLog("初始化布隆过滤器", "前次已经初始化");
            }
            if (!ComnTag.KeywordTag)
            {
                hp.InitKeywords();
                WriteLog.WriteWorkLog("初始化关键词列表", "成功完成！");
            }
            else
            {
                //WriteLog.WriteWorkLog("初始化关键词列表", "前次已经初始化");
            }

        }
        #endregion        

        #region 获取原始信息

        /// <summary>
        /// 通过正则表达式从网页源码中获取原始信息集合，如果网站配置要求保存原始信息，则在此方法内提供保存到数据库
        /// </summary>
        public List<InfoOriginal> GetListOriginal(WebsiteJK websiteJK)
        {
            string tmpStr = string.Format(websiteJK.RegexList);       //导入正则表达式
            string blockStr = GetBlockStr(websiteJK);       //获取网页源码中区域
            MatchCollection matches = Regex.Matches(blockStr, tmpStr, RegexOptions.IgnoreCase);
            List<InfoOriginal> lit = new List<InfoOriginal>();      ////定义集合对象
            
            if (matches.Count > 0)
            {
                for (int i = 0; i < matches.Count; i++)
                {
                    string url = websiteJK.PrefixUrl + matches[i].Groups["url"].Value.Trim();
                    if (HitPlan.bloomFilter.Contains(url))
                    {
                        //WriteLog.WriteWorkLog("原始列表中发现重复Url", url);
                        continue;
                    }
                    else
                    {
                        string title = matches[i].Groups["title"].Value.Trim();
                        title = HtmlRemoval.StripTagsCharArray(title);  // 去除字符串中的HTML代码
                        string author = matches[i].Groups["author"].Value.Trim();
                        string summary = matches[i].Groups["summary"].Value.Trim();
                        summary = HtmlRemoval.StripTagsCharArray(summary);  // 去除字符串中的HTML代码
                        //此处是针对微博信息，没有标题，只能从内容中提取前面若干字符作为标题
                        if (title.Length < 1 && summary.Length > 1)
                        {
                            if (summary.Length > 100)
                                title = summary.Substring(0, 100);
                            else
                                title = summary;
                        }
                        string public_time = matches[i].Groups["public_time"].Value.Trim();
                        InfoOriginal infoOriginal = new InfoOriginal() { WebsiteTypeId = websiteJK.WebsiteTypeId, WebsiteId = websiteJK.WebsiteId, BigclassId = websiteJK.Id, Url = url, Title = title, Author = author, Summary = summary, PublicTime = public_time};
                        lit.Add(infoOriginal);
                        if (websiteJK.IsSaveOriginalInfo)
                        {                            
                            try
                            {
                                InfoOriginalBLL.AddEntity(infoOriginal);  //加入数据库                                
                            }
                            catch (Exception ee)
                            {
                                //WriteLog.WriteErrLog("保存获取到网站(" + websiteJK.WebsiteId + "-" + websiteJK.BigclassName + ")的信息(标题："+infoOriginal.Url+")到数据库（原始列表）中错误", ee);//写入日志文件
                            }                                
                        }
                    }
                }
            }
            
            return lit;
        }

        //网页源码中包含贴子列表的一个大区块（正则表达式），如未能匹配，返回网页源码
        public static string GetBlockStr(WebsiteJK websiteJK)
        {
            string orgStr = HTTPHelper.GetHTMLByURL(websiteJK.BigclassUrl, websiteJK.Coding);
            if (websiteJK.Decryption == "unicode")
            {
                orgStr = GB2312UnicodeConverter.ToGB2312(orgStr);
            }
            //MessageBox.Show(tmpStr);  

            if (websiteJK.RegexBlock != null)
            {
                string tmpStr = string.Format(websiteJK.RegexBlock);
                MatchCollection matches = Regex.Matches(orgStr, tmpStr, RegexOptions.IgnoreCase);
                string result = orgStr;
                try
                {
                    result = matches[0].Value.Trim();
                }
                catch (Exception ee)
                {
                    WriteLog.WriteErrLog("获取网站(" + websiteJK.WebsiteId + "-" + websiteJK.BigclassName + ")源码中区块错误", ee);//写入日志文件
                }
                return result;
            }
            else
            {
                return orgStr;
            }
            
        }

        #endregion

        #region 获取命中信息

        /// <summary>
        /// 对从网页中获取的原始信息，进行命中比对，得到命中信息集合，保存到数据库
        /// </summary>
        public List<InfoHit> GetListHit(List<InfoOriginal> list)
        {
            List<InfoHit> lit = new List<InfoHit>();      ////定义集合对象            
            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)   //遍历元素
                {
                    InfoOriginal oreginal = list[i];
                    if (HitPlan.bloomFilter.Contains(oreginal.Url))     //布隆过滤,是否重复
                    {
                        //WriteLog.WriteWorkLog("命中列表中发现重复Url", oreginal.Url);
                        continue;
                    }                    
                    else
                    {
                        InfoHit hit = new InfoHit()
                        {
                            WebsiteTypeId = oreginal.WebsiteTypeId,
                            WebsiteId = oreginal.WebsiteId,
                            BigclassId = oreginal.BigclassId,
                            //CreatedOn = oreginal.CreatedOn,
                            Url = oreginal.Url,
                            Title = oreginal.Title,
                            Author = oreginal.Author,
                            Summary = oreginal.Summary,
                            PublicTime = oreginal.PublicTime
                        };
                        HitPlan.bloomFilter.Add(hit.Url);       //添加到布隆过滤器中
                        if (hp.HitPlanSJ(hit))    //如果命中关键词，加入list并存入数据库
                        {
                            lit.Add(hit);
                            try
                            {
                                InfoHitBLL.AddEntity(hit);   //加入数据库
                            }
                            catch (Exception ee)
                            {
                                //WriteLog.WriteErrLog("保存获取到网站(" + hit.WebsiteId + "-" + hit.BigclassId + ")的信息(标题：" + hit.Url + ")到数据库（命中列表）中错误", ee);//写入日志文件
                            }                            
                        }
                    }
                }
            }

            return lit;
        }

        #endregion        

        public static int test(string url)
        {
            return url.GetHashCode(); 
            //return HitPlan.bloomFilter.Contains(url);
        }
    }
}
